<? if ($c = stack('name')) {

	$_id_component	= $DATA['components'][$c]['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value
	
	$_crit = "id_data=$_id_data AND id_parent=$_id_component";
	
	$s = component_settings($c);



	$DATA['components'][$c]['data'] = array_mark_bounds( datafetch_array('db_list', array('table' => tb('data'), 'where' => "$_crit",	'order' => 'id_index', 'index' => 'id' )));

	foreach($DATA['components'][$c]['data'] as $k => $v) {
		$v['kind'] = $v['tag'] & 0xff;
		$v['required'] = ($v['tag'] >> 8) & 1;
	
		$v = array_merge($v, datafetch_array('meta', $_id_meta, $k, context('lang')));
		
		switch($v['kind']) {
			case 3: // select
				if ($p = $v['params']) 	// option field
					$v['options'] = explode("\n", $p);
				break;
			case 4:
				if ($p = $v['value']) 	// option field
					$v['options'] = array_trim_empty_str(explode(",", $p));
		}
			
		if ($v['required']) $v['title'].= '<strong>*</strong>';
	
		$DATA['components'][$c]['data'][$k] = $v;
	}


	if ((int) $_POST['check'] == $s['check']) {
	
		$req = array();
		foreach($DATA['components'][$c]['data'] as $k => $v) {
			if ($v['required'] && !postdata("field$k")) {
				$req[] = $v['title'];
			}
		}
		
		if (sizeof($req)) {
			diags('<p class="error">Error: Please fill the requested fields: <strong>'.implode(', ', $req).'.</strong></p>');
			return 1;
		}
	
		$res = array("You've got a new message!\r\n");
		$files = array();
	
		foreach($DATA['components'][$c]['data'] as $k => $v) {
			$v['title'] = str_replace('<strong>*</strong>', '', $v['title']);
			
			switch($v['kind']) {
				case 1: // text field
					$res[] = "\r\n$v[title]: ".$_POST["field$k"]."\r\n";
					break;
					
				case 2: // multiline text field
					$res[] = "\r\n$v[title]:\r\n".$_POST["field$k"]."\r\n";
					break;
					
				case 3:	// option list
	
					$t = "\r\n$v[title]:";
						foreach($_POST["field$k"] as $a)
							$t .= "\r\n".$v['options'][$a];
						if ($_POST["field$k"."_other"])
							$t .= "\r\n".$_POST["field$k"."_other"];
					$t .= "\r\n";
	
					$res[] = $t;
	
					break;
					
				case 4:
					if ($f = $_FILES["field$k"]) {
						$ext = parse_fname($f['name']);
						if (!(sizeof($v['options']) && (!$ext || in_array(strtolower($ext['ext']), $v['options'])))) {
							diags('<p class="error">Error: File types allowed are: <strong>'.implode(', ', $v['options']).'.</strong></p>');
							return 1;
						}
						
						$files[] = array('name' => $f['name'], 'data' => chunk_split(base64_encode(file_get_contents($f['tmp_name']))));
					}
					break;
			}
	
		}
	



		$subject = '=?utf-8?B?'.base64_encode( stripslashes("Feedback from the ".sta('project', 'company_short')." site")).'?=';
		
		$random_hash = md5(date('r', time())); 
		
		$headers = "From: $s[from]";
//		$headers .= "\r\nReply-To: \"=?utf-8?B?".base64_encode( stripslashes($_POST['name']))."?=\" <$_POST[mail]>";
		$headers .= "\r\nX-Mailer: PHP/".phpversion();
		$headers .= "\r\nMIME-Version: 1.0";
		$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"mixed-".$random_hash."\"";
		$headers .= "\r\nContent-Transfer-Encoding: base64";

		$body = chunk_split(base64_encode(stripslashes(implode("", $res))));


				ob_start();
?>
--mixed-<?=$random_hash?>

Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: base64

<?=$body?>

<? if (sizeof($files)) foreach($files as $f) { ?>
--mixed-<?=$random_hash?>

Content-Type: application/octet-stream; name="<?=$f['name']?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="<?=$f['name']?>"

<?=$f['data']?>

<? } ?>

--mixed-<?=$random_hash?>--

<?

		$body = ob_get_clean();
		
		if (@mail($s['contact'], $subject, $body, $headers))
			diags('<p class="success">Your message has been sent.</p>');
		else
			diags('<p class="error">Error: Mail sending failed. Please try again in a few minutes.</p>');
	}


	$GLOBALS['SP']--;
}

?>